package fti.uksw.ruang.websocket;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;

public class WebSocketClient {

	private static final int OPENED = 0;
	private static final int CLOSED = 1;
	private static final int MESSAGE_TEXT = 2;
	private static final int MESSAGE_BYTE = 3;

	private WebSocketConnection mConnection = new WebSocketConnection();

	private String defIPServer = "192.168.67.6";
	private String defPort = "8082";

	private String wsUri = "ws://" + defIPServer + ":" + defPort
			+ "/lab-server/lab";

	private static List<ClientListener> mListener = new ArrayList<ClientListener>();

	public boolean isConnected() {
		return mConnection.isConnected();
	}

	public void addListener(ClientListener aListener) {
		mListener.add(aListener);
	}

	public void removeListener(ClientListener aListener) {
		mListener.remove(aListener);
	}

	public void setWsUri(String ip, String port) {
		wsUri = "ws://" + ip + ":" + port + "/lab-server/lab";
	}

	public void startConnection() {
		try {
			mConnection.connect(wsUri, new WebSocketHandler() {
				public void onOpen() {
					Message lmsg = new Message();
					lmsg.what = OPENED;
					lmsg.obj = "Connected to " + wsUri;
					messageHandler.handleMessage(lmsg);
				}

				public void onTextMessage(String payload) {
					Message lmsg = new Message();
					lmsg.what = MESSAGE_TEXT;
					lmsg.obj = payload;
					messageHandler.handleMessage(lmsg);
				}

				@Override
				public void onBinaryMessage(byte[] payload) {
					Message lmsg = new Message();
					lmsg.what = MESSAGE_BYTE;
					lmsg.obj = payload;
					messageHandler.handleMessage(lmsg);
				}

				public void onClose(int code, String reason) {
					Message lmsg = new Message();
					lmsg.what = CLOSED;
					lmsg.obj = code + ";" + reason;
					messageHandler.handleMessage(lmsg);
				}

			});
		} catch (WebSocketException e) {
			e.printStackTrace();
		}
	}

	private static void notifyOpened(String message) {
		for (ClientListener listener : mListener) {
			listener.onConnect(message);
		}
	}

	private static void notifyClosed(String message) {
		for (ClientListener listener : mListener) {
			listener.onDisconnect(message);
		}
	}

	private static void notifyMessage(String message) {
		for (ClientListener listener : mListener) {
			listener.onMessage(message);
		}
	}

	private static void notifyMessage(byte[] message) {
		for (ClientListener listener : mListener) {
			listener.onMessage(message);
		}
	}

	private static Handler messageHandler = new Handler() {
		public void handleMessage(Message aMessage) {
			switch (aMessage.what) {
			case OPENED:
				notifyOpened(aMessage.obj.toString());
				break;
			case CLOSED:
				notifyClosed(aMessage.obj.toString());
				break;
			case MESSAGE_TEXT:
				notifyMessage(aMessage.obj.toString());
			case MESSAGE_BYTE:
				try {
					notifyMessage(aMessage.obj.toString().getBytes("UTF-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		}
	};

	public void sendTextMessage(String message) {
		mConnection.sendTextMessage(message);
	}

	public void closeConnection() {
		mConnection.disconnect();
	}

	public static Handler getMessageHandler() {
		return messageHandler;
	}

}
